package com.sjtucook;



import com.utl.MyReceiver;
import com.utl.OAuth;
import com.weibo.WeiboAuthorizeActivity;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;






import com.weibo.WeiboMainActivity;

import android.app.Activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

public class DishDetailActivity extends Activity {
	private Button search;
	private Button addfavorite;
	private Button share;
	private Button comment;
	private Button back;
	private LinearLayout content;

	private Dialog pingfendialog;
	private RatingBar ratingbar;


	private Bundle bundle;

	private CookApplication currentApp;

	private Dish dish;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.content);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.titlebar);
		bundle = this.getIntent().getExtras();
		dish = (Dish) bundle.getParcelable(DishListActivity.DISH);
		dish.setRate(dish.getRate() + 1);
		// get CookApplication object
		currentApp = (CookApplication) this.getApplicationContext();
		// db query
		currentApp.getDbadapter().updateHistory(dish);
		currentApp.getDbadapter().updateDish(dish);
		content = (LinearLayout) findViewById(R.id.center_pane);
		LinearLayout detail = (LinearLayout) this.getLayoutInflater().inflate(
				R.layout.detail, null);
		content.addView(detail);

		TextView view = (TextView) findViewById(R.id.detail_right);
		view.setText(dish.getName() + "\n材料: " + dish.getMaterial());
		view = (TextView) findViewById(R.id.detail_bottom);

		view.setText("制作过程: " + dish.getDescription());


		// search button
		this.search = (Button) findViewById(R.id.tittle_bt);
		this.search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onSearchRequested();
			}
		});
		// favorite button
		this.addfavorite = (Button) findViewById(R.id.bottom_button_1);
		if (dish.getFavorite() == 0) {
			this.addfavorite.setText(R.string.ADD_FAVORITE);
		} else {
			this.addfavorite.setText(R.string.REMOVE_FAVORITE);
		}
		this.addfavorite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (dish.getFavorite() == 0) {
					dish.setFavorite(1);
					addfavorite.setText(R.string.REMOVE_FAVORITE);
				} else {
					dish.setFavorite(0);
					addfavorite.setText(R.string.ADD_FAVORITE);
				}
				currentApp.getDbadapter().updateDish(dish);

			}

		});
		// toprated button
		this.share = (Button) findViewById(R.id.bottom_button_2);
		this.share.setText(R.string.SHARE);
		this.share.setOnClickListener(new OnClickListener() {
			// share a picture
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				/* 开启Pictures画面Type设定为image */
				intent.setType("image/*");
				/* 使用Intent.ACTION_GET_CONTENT这个Action */
				intent.setAction(Intent.ACTION_GET_CONTENT);
				/* 取得相片后返回本画面 */
				startActivityForResult(intent, 1);
			}

		});
		// history button
		this.comment = (Button) findViewById(R.id.bottom_button_3);
		this.comment.setText(R.string.COMMENT);
		this.comment.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				View diaView = View.inflate(DishDetailActivity.this,
						R.layout.weibopingfen, null);
				pingfendialog = new Dialog(DishDetailActivity.this,
						R.style.pingfendialog);
				pingfendialog.setContentView(diaView);
				pingfendialog.show();

				ratingbar = (RatingBar) diaView.findViewById(R.id.ratingBar1);
				ratingbar.setNumStars(3); // 设置总分为3分
				ratingbar.setRating(1); // 初始值为1分
				
				
				Button Btn_sendWeibo = (Button) diaView
						.findViewById(R.id.button_shareweibo);

				Btn_sendWeibo.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {

						
						String weibomsg = "大家好，我正在用Cook软件浏览美食!" + " 美食名称:"
								+ dish.getName() + " 材料:" + dish.getMaterial()
								+ " 做法:" + dish.getDescription() + " 我的打分是:"
								+ String.valueOf(ratingbar.getRating() + "颗星");
						MyReceiver.msg=weibomsg;
						
						Intent intent = new Intent();

						intent.setClass(DishDetailActivity.this,
								WeiboMainActivity.class);
						startActivity(intent);

					}

				});

			}

		});
		// update button
		this.back = (Button) findViewById(R.id.bottom_button_4);
		this.back.setText(R.string.BACK);
		this.back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				onBackPressed();

			}

		});
	}

	// 当用户点击 分享 按钮，得到照片，返回该函数
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Uri uri = data.getData();
			Log.e("uri", uri.toString());
			ContentResolver cr = this.getContentResolver();
			try {
				Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
				
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
			    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
			   
			    MyReceiver.dat = baos.toByteArray();
				MyReceiver.haspic=true;
				MyReceiver.msg="这是我拍的照片哦，喜欢么？";
				
				Intent intent = new Intent();

				intent.setClass(DishDetailActivity.this,
						WeiboMainActivity.class);
				startActivity(intent);
				
				
			} catch (FileNotFoundException e) {
				Log.e("Exception", e.getMessage(),e);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	
	public byte[] getBitmapByte(Bitmap bitmap){   
	    ByteArrayOutputStream out = new ByteArrayOutputStream();   
	    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);   
	    try {   
	        out.flush();   
	        out.close();   
	    } catch (IOException e) {   
	        e.printStackTrace();   
	    }   
	    return out.toByteArray();   
	}   

}
